package com.zsxb.crm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zsxb.crm.dto.ClueReportDTO;
import com.zsxb.crm.dto.ContractPageDTO;
import com.zsxb.crm.mapper.*;
import com.zsxb.crm.pojo.CluesReportVO;
import com.zsxb.crm.pojo.Contract;
import com.zsxb.crm.pojo.NewUserReport;
import com.zsxb.crm.pojo.SaleReport;
import com.zsxb.crm.result.PageResult;
import com.zsxb.crm.service.ReportService;
import com.zsxb.crm.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ReportServiceimpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;

    /**
     * 合同数据统计分页
     */
    @Override
    public PageResult contractStatisticsListPageQuery(ContractPageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(),pageDTO.getPageSize());
        Page<Contract> page = contractMapper.report(pageDTO);
        return new PageResult(page.getTotal(),page.getResult(),null);
    }

    /**
     * 获取全部日期
     */
    private List<LocalDate> getDateList(LocalDate beginDate, LocalDate endDate){
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(beginDate);
        while (!beginDate.equals(endDate)){
            beginDate = beginDate.plusDays(1);
            dateList.add(beginDate);
        }
        return dateList;
    }

    /**
     * 用户数据统计
     */
    @Override
    public NewUserReport contractStatistics(LocalDate beginDate, LocalDate endDate) {
        List<LocalDate> xAxis = getDateList(beginDate, endDate);
        List<UserReportVO> userReport =  reportMapper.userStatistics(beginDate,endDate);
        // 封装data:newUser, data:totalUser数据
        List<Integer> newUserList = userReport.stream().map(UserReportVO::getNewUser).toList();
        List<Integer> totalUserList = userReport.stream().map(UserReportVO::getTotalUser).toList();
        List<Map<String, Object>> series = new ArrayList<>();
        Map<String, Object> s1 = new HashMap<>();
        s1.put("name", "新增客户数");
        series.add(s1);
        Map<String, Object> s2 = new HashMap<>();
        s2.put("data", newUserList);
        series.add(s2);
        Map<String, Object> s3 = new HashMap<>();
        s1.put("name", "客户总数");
        series.add(s3);
        Map<String, Object> s4 = new HashMap<>();
        s1.put("data", totalUserList);
        series.add(s4);
        return new NewUserReport(xAxis,series);
    }

    /**
     * 学科数据统计
     */
    @Override
    public List<Map<String, Object>> subjectStatistics(LocalDate beginCreateTime, LocalDate endCreateTime) {
        List<SubjectVO> subjectReport = reportMapper.subjectStatistics(beginCreateTime,endCreateTime);
        List<Map<String, Object>> subjectList = new ArrayList<>();
        // 封装学科:用户数据
        for (SubjectVO subjectVO : subjectReport) {
            Map<String, Object> m = new HashMap<>();
            m.put("subject", subjectVO.getSubject());
            m.put("num", subjectVO.getNum());
            subjectList.add(m);
        }
        return subjectList;
    }

    /**
     * 部门查询
     */
    @Override
    public PageResult deptStatisticsList(ContractPageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(),pageDTO.getPageSize());
        Page<DeptStatisticsListVO> page = (Page<DeptStatisticsListVO>) reportMapper.deptStatisticsList(pageDTO);
        return new PageResult(page.getTotal(),page.getResult(),"查询成功");
    }

    @Override
    public SaleReport salesStatistics(LocalDate beginDate, LocalDate endDate) {
        List<LocalDate> xAxis = getDateList(beginDate, endDate);
        List<ReportSalesVO> salesList = reportMapper.salesStatistics(beginDate,endDate);
        List<Integer> list = new ArrayList<>();
        for (LocalDate day : xAxis) {
            ReportSalesVO reportSalesVO = salesList.stream().filter(s -> s.getDate().equals(day)).findFirst().orElse(null);
            if (reportSalesVO == null){
                list.add(0);
            }else {
                list.add(reportSalesVO.getOrderPrice());
            }
        }
        List<Map<String, Object>> series1 = new ArrayList<>();
        Map<String, Object> series = new HashMap<>();
        series.put("name","销售统计");
        series.put("data", list);
        series1.add(series);
        return new SaleReport(xAxis,series1);
    }


    @Override
    public PageResult clueStatistics(ClueReportDTO clueReportDTO) {
        PageHelper.startPage(clueReportDTO.getPageNum(),clueReportDTO.getPageSize());
        Page<CluesReportVO> clues = (Page<CluesReportVO>) reportMapper.clueStatistics(clueReportDTO);
        return new PageResult(clues.getTotal(),clues.getResult(),null);
    }


    @Override
    public SaleReport cluesStatistics(LocalDate beginDate, LocalDate endDate) {
        List<LocalDate> xAxis = getDateList(beginDate, endDate);
        List<ClueReportVO> clueList = reportMapper.cluesStatistics(beginDate,endDate);
        // 封装data:新增线索数量, data:线索总数量数据
        List<Integer> newClueList = clueList.stream().map(ClueReportVO::getNewClue).toList();
        List<Integer> totalClueList = clueList.stream().map(ClueReportVO::getTotalClue).toList();
        List<Map<String, Object>> series = new ArrayList<>();
        Map<String, Object> s1 = new HashMap<>();
        s1.put("name", "新增线索数量");
        series.add(s1);
        Map<String, Object> s2 = new HashMap<>();
        s2.put("data", newClueList);
        series.add(s2);
        Map<String, Object> s3 = new HashMap<>();
        s1.put("name", "线索总数量数据");
        series.add(s3);
        Map<String, Object> s4 = new HashMap<>();
        s1.put("data", totalClueList);
        series.add(s4);
        return new SaleReport(xAxis,series);
    }


    @Override
    public List<Map<String, Object>> chanelStatistics(LocalDate beginCreateTime, LocalDate endCreateTime) {
        List<ChannelReportVO> channelReport = reportMapper.chanelStatistics(beginCreateTime,endCreateTime);
        List<Map<String, Object>> channelList = new ArrayList<>();
        // 封装channel:num
        for (ChannelReportVO c : channelReport) {
            Map<String, Object> m = new HashMap<>();
            m.put("num", c.getNum());
            m.put("channel", c.getChannel());
            channelList.add(m);
        }
        return channelList;
    }

    @Override
    public VulnerabilityMap getVulnerabilityMap(LocalDate beginDate, LocalDate endDate) {
        Integer effectiveCluesNums = clueMapper.queryuserfulClueByDate(beginDate.atStartOfDay(), endDate.atStartOfDay());
        Integer cluesNums = clueMapper.queryNewClueByDate(beginDate.atStartOfDay(), endDate.atStartOfDay());
        Integer businessNums = businessMapper.selectCount(null);
        Integer contractNums = reportMapper.querycontractNumsByDate(beginDate,endDate);
        return new VulnerabilityMap(cluesNums,effectiveCluesNums,businessNums,contractNums);
    }


}
